
#include "main.h"

enum
{
	SENDER_RESET = 0,
	REQUEST_TO_MODEM,
	DELAY,
	WAIT_FOR_MODEM_ANSWER
};

FUNCTION* Tools;
FUNCTION* ToolsReturn;

void SMS_Sender ( void );

unsigned char SMS_Sender__PhoneNumber[CALL_NUMBER_SIZE];
unsigned char SMS_Sender__Status;
//unsigned char SMS_Sender__Message[120];

//unsigned char SMS_Sender_Manager__State = READY; /* send if State == READY */
unsigned char SMS_Sender_Manager__Status; 
unsigned char SMS_Sender_Manager__Message[120] = " <<< SMS TEXT >>>";
unsigned int  SMS_Sender_Manager__SendTo;	/* Send to: -> List address of OWNERS or USERS */
//unsigned char SMS_Sender_Manager__MessageType; /* "ALARM",  "END OF ALARM"  */
unsigned char SMS_Sender_Result;
/* unsigned char SMS_Recipient_Phone[24] = "EMPTY SMS"; */

unsigned char SMS_Add_Message ( char* RecipientPhone, char* Message )
{
	if ( RecipientPhone == NULL || Message == NULL )
	{
		return SMS_SENDER_ERROR__DATA; 
	}  
	
	if ( Tools != NULL )
	{
		return SMS_SENDER_ERROR__SENDER_BUSY;  
	}

	if ( RecipientPhone[0] == 'L' ) /* send to numbers from the list */
	{
		SMS_Sender_Manager__SendTo = ListNumber_ToAddress(StrToLong(RecipientPhone));

		if ( SMS_Sender_Manager__SendTo == UNKNOWN_GROUP )
		{
			Debug("> SMS_Add_Message - UNKNOWN GROUP: %d", SMS_Sender_Manager__SendTo); 
			return SMS_SENDER_ERROR__UNKNOW_LIST;
		}

		Tools = SMS_Sender_Manager;
	}
	else								  /* Send to number  */
	{
		if ( StrLen(RecipientPhone)  )	/* use old phone number  */
		{ 
			if ( StrLen(RecipientPhone) < CALL_NUMBER_SIZE )
			{
				StrCpy(SMS_Sender__PhoneNumber, RecipientPhone);
			}
			else
			{
				return SMS_SENDER_ERROR__DATA; 
			}
		}
		else
			if ( StrLen(SMS_Sender__PhoneNumber) == 0 )
			{
				return SMS_SENDER_ERROR__NO_PHONE;
			}

		ToolsReturn = NULL;
		Tools = SMS_Sender;
	}

	Write_SMS(Message);

	Debug("\nAdd_SMS send to: %s\r", SMS_Sender__PhoneNumber);
	Debug("\nAdd_SMS message: %s\r", SMS_Sender_Manager__Message);
		
	/* See result in  SMS_Sender__Status;  positive result == SUCCESS */

	return 0;	
}


void Write_SMS ( unsigned char* Message )
{
	unsigned char* p = FindCharNumber(Message, ']', 1);
	
	if ( p == NULL )
	{
		StrCpy(SMS_Sender_Manager__Message, Message);
	}
	else
	{
		StrCpy(SMS_Sender_Manager__Message, p);
	}

	Debug("\nWrite_SMS: %s", SMS_Sender_Manager__Message);
}

void SMS_Sender_Manager( void )
{
	static unsigned int Address, Amount, EntryIndex;//, i; 
	static unsigned char  ErrCnt, State = ST_START;
//	static FUNCTION* MyCaller;
	RECORD_CUSTOMER Entry;

	SMS_Sender_Manager__Status = BUSY;

	switch ( State )
	{
		case ST_START:
			Debug("\n> SMS_Sender_Manager: started\r");

			if ( SMS_Sender_Manager__SendTo == LIST_OWNERS )
			{
				Amount = COUNT_OWNERS;
			}
			else
				if ( SMS_Sender_Manager__SendTo == LIST_USERS_GROUP_1 ||
					  SMS_Sender_Manager__SendTo == LIST_USERS_GROUP_2 ||
					  SMS_Sender_Manager__SendTo == LIST_USERS_GROUP_3 ||
					  SMS_Sender_Manager__SendTo == LIST_USERS_GROUP_4 )
					
				{
					Amount = COUNT_USERS;
				}
				else
				{
					Debug("\n> SMS_Sender_Manager ERROR: Unknow list number\r");
					SMS_Sender_Manager__Status = FAIL; /* Unknow list number */
					break;
				}

			ErrCnt = 8;
			EntryIndex = 0;
			
			Address = SMS_Sender_Manager__SendTo; /* List address */
			State = 1;
			
			break;
	

		case 1:
			for ( ; EntryIndex < Amount;  EntryIndex++ )
			{
				if ( Ex_ReadByte(Address) != 0xFF )	/* Check if the Entry is not empty */
				{
					Ex_LoadFrom((unsigned char*)&Entry, Address, sizeof(Entry));
					FlipFlopN(Entry.Phone, PHONE_NUMBERS_BCD_SIZE);
					BCDToStr(SMS_Sender__PhoneNumber, Entry.Phone, PHONE_NUMBERS_BCD_SIZE );  
					ToolsReturn = SMS_Sender_Manager; 
					Tools = SMS_Sender; 																/* Sender start */
					State = 2;
					return; 
				}
				Address += sizeof(RECORD_CUSTOMER);
			}

			SMS_Sender_Manager__Status = SUCCESS; /* end of List */
			Debug(">> SMS_Sender_Manager: END OF LIST\r", SMS_Sender_Manager__Status);
			break;

		case 2:
			if ( SMS_Sender__Status != SUCCESS )
			{
				if ( --ErrCnt == 0 )
				{
					SMS_Sender_Manager__Status = FAIL; /* much ERRORs */
				}
				Debug(">> SMS_Sender_Manager: SMS_Sender - ERROR", SMS_Sender__Status);

			}
			else
			{
				Address += sizeof(RECORD_CUSTOMER); /* send message to next address  */
				EntryIndex++;
			}
			State = 1;
			break;

		default: 
			State = ST_START;
	}

	if ( SMS_Sender_Manager__Status != BUSY )
	{
		SMS_Sender_Result = SMS_Sender_Manager__Status;
		State = ST_START;
		Tools = NULL;
	}			
}

void SMS_Sender ( void )
{
	static unsigned char State = 0, TryIfNotResponse;
	static unsigned char TimeOut, Delay;

	SMS_Sender__Status = BUSY;
	
	switch ( State )
	{
		case SENDER_RESET:
			if ( ModemStatus == READY ) 
			{
				ModemStatus = BUSY_SMS_SENDER;
				TimeOut = 20;
				TryIfNotResponse = 3;
				State = REQUEST_TO_MODEM;
			}
			break;
			
		case REQUEST_TO_MODEM:
			
			putsf("AT+CMGS=\"", MODEM_PORT);
			puts(SMS_Sender__PhoneNumber, MODEM_PORT);

			Debug("\nSMS_Sender; Send to: %s\n", SMS_Sender__PhoneNumber);
						
			if ( SMS_Sender__PhoneNumber[0] == '+' )
				putsf("\",145\r", MODEM_PORT );
			else
				putsf("\",129\r", MODEM_PORT );
			
			Delay = 20;
			State = DELAY;
			break;

		case DELAY:
			if ( Time_10ms && --Delay == 0 )
			{
				puts(SMS_Sender_Manager__Message, MODEM_PORT);

				Debug("\nSMS_Sender; Message: %s\n", SMS_Sender_Manager__Message);
				
				putsf("\x1A", MODEM_PORT);

				State = WAIT_FOR_MODEM_ANSWER;
			}
			break;

		case WAIT_FOR_MODEM_ANSWER:
			if ( ModemEvents == 0 )
			{
				if ( Time_1000ms && --TimeOut == 0 )
				{
					if ( --TryIfNotResponse == 0 )
					{
						ModemStatus = MODEM_NOT_ANSWER;

						Debug(">> ERROR: MODEM_NOT_ANSWER");

						SMS_Sender__Status = FAIL; //MODEM_NOT_ANSWER;
					}	
					else
					{
						TimeOut = 8;
						State = REQUEST_TO_MODEM;
					}
				}
			}	 
			else
				if ( ModemEvents == MODEM_NEW_MESSAGE_COMPLETE )
				{
					SMS_Sender__Status = SUCCESS;
				}	
				else
					if ( ModemEvents == MODEM_ERROR )
					{
						Debug(">> MODEM_ERROR");

						delay_ms(3000);
						SMS_Sender__Status = FAIL;
					}
					else
						if ( ModemEvents == NO_CARRIER )
						{
							Debug(">> ERROR: MODEM_NO_CARRIER");
							SMS_Sender__Status = NO_CARRIER;
						}
			
			break;
	

		default:
			;
	}

	if ( SMS_Sender__Status != BUSY )
	{
		State = SENDER_RESET;
		Tools = ToolsReturn;
		ModemStatus = READY;

		SMS_Sender_Result = SMS_Sender__Status;
		
/* 		if ( ModemStatus != MODEM_NOT_ANSWER ) */
/* 			ModemStatus = READY; */
/* 		else */
/* 			RESET(); */
	}
}
